//
//  MineViewController.m
//  YTwallet
//
//  Created by hadrlog on 2019/2/22.
//  Copyright © 2019年 hadrlog. All rights reserved.
//

#import "MineViewController.h"
#import "Mineview.h"
#import "MineTableViewCell.h"
//设置页面
#import "SettingViewController.h"
//登录
#import "LoginViewController.h"
#import "NavigateViewController.h"
#import "AppTools.h"
//实名认证
#import "AuthenViewController.h"
//我的银行卡
#import "MyBankCardViewController.h"
//账单查询
#import "BillSelectViewController.h"

//帮助中心
#import "MineHelpCenterViewController.h"
//查看收益
#import "EarningsDetailViewController.h"
//查看团队
#import "MyTeamViewController.h"

//指纹或手势登录
#import "GestureViewController.h"

//基础实名认证展示
#import "AuthenResultInfoViewController.h"
#import "GetUserInfoModelPack.h"
//实名认证展示页面
#import "AuthenResultViewController.h"

@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)Mineview *mv;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *imageArray0;
@property(nonatomic,strong)NSMutableArray *titleArray0;
@property(nonatomic,strong)NSMutableArray *imageArray1;
@property(nonatomic,strong)NSMutableArray *titleArray1;
@property(nonatomic,strong)NSMutableArray *imageArray2;
@property(nonatomic,strong)NSMutableArray *titleArray2;

@end

@implementation MineViewController

-(NSMutableArray *)imageArray0
{
    if (_imageArray0 == nil) {
        _imageArray0 = [NSMutableArray array];
    }
    return _imageArray0;
}
-(NSMutableArray *)titleArray1
{
    if (_titleArray1 == nil) {
        _titleArray1 = [NSMutableArray array];
    }
    return _titleArray1;
}
-(NSMutableArray *)imageArray2
{
    if (_imageArray2 == nil) {
        _imageArray2 = [NSMutableArray array];
    }
    return _imageArray2;
}
-(NSMutableArray *)titleArray0
{
    if (_titleArray0 == nil) {
        _titleArray0 = [NSMutableArray array];
    }
    return _titleArray0;
}
-(NSMutableArray *)imageArray1
{
    if (_imageArray1 == nil) {
        _imageArray1 = [NSMutableArray array];
    }
    return _imageArray1;
}
-(NSMutableArray *)titleArray2
{
    if (_titleArray2 == nil) {
        _titleArray2 = [NSMutableArray array];
    }
    return _titleArray2;
}

#pragma mark 初始化
-(void)loadView{
    [super loadView];
    self.mv = [[Mineview alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.mv];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //等级赋值
    NSString *levelId = [AppTools shareInstance].userInfo.levelId;
    NSString *str;
    if ([@"100001" isEqualToString:levelId]) {
        str = @" 普通会员 ";
    }else if ([@"100002" isEqualToString:levelId])
    {
        str = @" 高级会员 ";
    }else if ([@"100003" isEqualToString:levelId])
    {
        str = @" 至尊会员 ";
    }
    
    
    
    [self.mv.levelBtn setTitle:str forState:(UIControlStateNormal)];
    
    //信用卡储蓄卡数量赋值
    //只更新第一条cell
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
    //检查是否登录
    [self setupUIAndReloadData];
    
}
-(void)setupUIAndReloadData
{
    if (![AppTools isLogin]) {                                        // 未登录
        NSLog(@"未登录");
        self.mv.noLoginView.hidden = NO;
        self.mv.loginSuccView.hidden = YES;
        self.mv.loginSuccAndBasicView.hidden = YES;
        
        return;
    }
    if([AppTools checkLoginResponseAllState] == ETNOAutonym)               //已登录未实名
    {
        NSLog(@"已登录未实名");
        self.mv.noLoginView.hidden = YES;
        self.mv.loginSuccView.hidden = NO;
        self.mv.loginSuccAndBasicView.hidden = YES;
        NSString *phoneNumStr = [AppTools shareInstance].userInfo.loginName;
        if (phoneNumStr.length == 11) {
            self.mv.loginSucphoneNumLab.text = [self changeTelephone:phoneNumStr];
        }
    }else if([AppTools checkLoginResponseAllState] == ETBasicAuthentication)     //已登录完成基础认证
    {
        NSLog(@"已登录完成基础认证");
        self.mv.noLoginView.hidden = YES;
        self.mv.loginSuccView.hidden = YES;
        self.mv.loginSuccAndBasicView.hidden = NO;
        self.mv.nameLab.text = [AppTools shareInstance].userInfo.userName;
        NSString *phoneNumStr = [AppTools shareInstance].userInfo.loginName;
        self.mv.phoneNumLab.text = [self changeTelephone:phoneNumStr];
        //基础认证详情
        [self.mv.basicAuthenedBtn addTarget:self action:@selector(basicAuthenedBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
        [self.mv.arrowBtn addTarget:self action:@selector(basicAuthenedBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
        
    }
}
-(void)basicAuthenedBtnAction
{
    NSLog(@"点击已基础认证");

    NSString *url = [[ServerAddressModel shareInstance] fullTAnd0AddrWithParameter:@"/epay_apps/user/userInfoE0100"];
    [[ETONEHttpClient sharedInstance] doHTTPSPOST:url Parameters:nil HttpBody:nil SuccessHandler:^(id responseObject) {
        
        NSDictionary *dic = [responseObject mj_JSONObject];
        NSString *code = dic[@"code"];
        if ([@"0000" isEqualToString:code]) {
            [self dealUserInfoWithresponse:responseObject];
        }
        
    } FailedHandler:^(NSError *error) {
        
    }];
}
-(void)dealUserInfoWithresponse:(id)responseObject
{
    
    GetUserInfoResponse *getUserInfoResponse = [GetUserInfoResponse mj_objectWithKeyValues:responseObject];
    GetUserInfoResponseData *userInfo = getUserInfoResponse.response;
    

    AuthenResultViewController *authenResultVC = [[AuthenResultViewController alloc] init];
    authenResultVC.accountName = userInfo.userName;
    authenResultVC.mobile = userInfo.mobile;
    authenResultVC.crpIdenNumber = userInfo.crpIdenNumber;
    
    authenResultVC.accountName = userInfo.accountName;
    authenResultVC.bankReservedMobile = userInfo.bankReservedMobile;
    authenResultVC.accountNo = userInfo.accountNo;
    authenResultVC.openBankName = userInfo.openBankName;
    [self.navigationController pushViewController:authenResultVC animated:YES];
}


//手机号隐藏中间4位
-(NSString *)changeTelephone:(NSString *)teleStr
{
    
    if (teleStr.length != 11) {
        return teleStr;
    }

   NSString * string = [teleStr stringByReplacingOccurrencesOfString:[teleStr substringWithRange:NSMakeRange(3,4)]withString:@"****"];
    return string;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //右上角设置
    [self createRightBtn];
    //创建tableView
    [self createTableView];
    self.view.backgroundColor = [UIColor colorWithRed:242 / 255.0 green:244 / 255.0 blue:246 / 255.0 alpha:1];
    //登录注册
    [self.mv.loginOrRegisterBtn addTarget:self action:@selector(loginOrRegisterBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    //实名认证
    [self.mv.loginSuctoAutonymBtn addTarget:self action:@selector(toAutonymBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
}
-(void)toAutonymBtnAction
{
    NSLog(@"去实名认证");
    AuthenViewController *authenVC = [[AuthenViewController alloc] init];
    [self.navigationController pushViewController:authenVC animated:YES];
}
-(void)loginOrRegisterBtnAction
{
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    NavigateViewController *nav = [[NavigateViewController alloc] initWithRootViewController:loginVC];
    [self presentViewController:nav animated:YES completion:nil];
    
    
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}
-(void)createTableView
{
    self.imageArray0 = @[@"me_ic_yinhangka",@"me_ic_ pos",@"me_ic_bill"].mutableCopy;
    self.titleArray0 = @[@"我的银行卡"].mutableCopy;
    self.imageArray1 = @[@"me_ic_bill",@"me_ic_profit",@"me_ic_team"].mutableCopy;
    self.titleArray1 = @[@"账单查询",@"查看收益",@"查看团队"].mutableCopy;
    self.imageArray2 = @[@"me_ic_help",@"me_ic_contact"].mutableCopy;
    self.titleArray2 = @[@"帮助中心",@"联系我们"].mutableCopy;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, ETScreenH * 0.15, self.view.frame.size.width, ETScreenH - ETScreenH * 0.15 - 49 - 64) style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithRed:242 / 255.0 green:244 / 255.0 blue:246 / 255.0 alpha:1];
//    self.tableView.scrollEnabled = NO;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.mv addSubview:self.tableView];
    [self.tableView registerClass:[MineTableViewCell class] forCellReuseIdentifier:@"cell"];
}

-(void)createRightBtn
{
    UIButton *rightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    rightBtn.frame = CGRectMake(0, 0, 40, 20);
    [rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    [rightBtn setImage:[UIImage imageNamed:@"nav_icon_set"] forState:(UIControlStateNormal)];
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
}
-(void)rightBtnAction
{
    NSLog(@"点击设置");
    if (![AppTools isLogin]) {
        //跳转到登录页面
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        NavigateViewController *nav = [[NavigateViewController alloc] initWithRootViewController:loginVC];
        [self presentViewController:nav animated:YES completion:nil];
        return;
    }else{
        SettingViewController *settingVC = [[SettingViewController alloc] init];
        [self.navigationController pushViewController:settingVC animated:YES];
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 3;
            break;
        case 2:
            return 2;
            break;
            
        default:
            break;
    }
    
    return 0;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        cell.imv.image = [UIImage imageNamed:self.imageArray0[indexPath.row]];
        cell.titleLab.text = self.titleArray0[indexPath.row];
        if (indexPath.row == 0) {
            NSString *credicCardNum = [AppTools shareInstance].userInfo.creditCardNum;
            NSString *cdrdsNumNum = [AppTools shareInstance].userInfo.cdrdsNum;
            
            
            if (credicCardNum == nil || credicCardNum == NULL) {
                credicCardNum = @"0";
            }
            
            if (cdrdsNumNum == nil || cdrdsNumNum == NULL) {
                cdrdsNumNum = @"0";
            }
            
            cell.tipsLab.text = [NSString stringWithFormat:@"%@张储蓄卡%@张支付卡",cdrdsNumNum,credicCardNum];
        }else if (indexPath.row == 1)
        {
            cell.tipsLab.text = @"0台POS机";
            cell.lineView.hidden = YES;
        }
        
    }else if (indexPath.section == 1)
    {
        cell.imv.image = [UIImage imageNamed:self.imageArray1[indexPath.row]];
        cell.titleLab.text = self.titleArray1[indexPath.row];
        if (indexPath.row == 2) {
            cell.lineView.hidden = YES;
        }
        
        
    }else if (indexPath.section == 2)
    {
        cell.imv.image = [UIImage imageNamed:self.imageArray2[indexPath.row]];
        cell.titleLab.text = self.titleArray2[indexPath.row];
        if (indexPath.row == 1) {
            cell.lineView.hidden = YES;
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //判断登录状态  除了"联系我们"可以点击
    if (indexPath.section == 2 && indexPath.row == 1) {
        //不处理
    }else
    {
        if (![AppTools isLogin]) {
            //跳转到登录页面
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            NavigateViewController *nav = [[NavigateViewController alloc] initWithRootViewController:loginVC];
            [self presentViewController:nav animated:YES completion:nil];
            return;
        }
        
    }
    
    
    
    //我的银行卡
    if (indexPath.section == 0 && indexPath.row == 0) {
        //判断实名认证状态
        if ([AppTools checkLoginResponseAllState] == ETNOAutonym) {
            [self showFailedResultTips:@"请先实名认证" animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
            return;
        }
        
        MyBankCardViewController *myBankCardVC = [[MyBankCardViewController alloc] init];
        [self.navigationController pushViewController:myBankCardVC animated:YES];
    }
    //我的POS机
    if (indexPath.section == 0 && indexPath.row == 1) {
        
        [self showFailedResultTips:ETNODisparkStr animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
        
        /*
        //判断实名认证状态
        if ([AppTools checkLoginResponseAllState] == ETNOAutonym) {
            [self showFailedResultTips:@"请先实名认证" animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
            return;
        }
         */
        
    }
    
    
    //账单查询
    if (indexPath.section == 1 && indexPath.row == 0) {
        
        //判断实名认证状态
        if ([AppTools checkLoginResponseAllState] == ETNOAutonym) {
            [self showFailedResultTips:@"请先实名认证" animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
            return;
        }
        
        BillSelectViewController *billSelectVC = [[BillSelectViewController alloc] init];
        [self.navigationController pushViewController:billSelectVC animated:YES];
    }
    //查看收益
    if (indexPath.section == 1 && indexPath.row == 1) {
        
        //判断实名认证状态
        if ([AppTools checkLoginResponseAllState] == ETNOAutonym) {
            [self showFailedResultTips:@"请先实名认证" animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
            return;
        }
        
        EarningsDetailViewController *earnVC = [[EarningsDetailViewController alloc] init];
        [self.navigationController pushViewController:earnVC animated:YES];
    }
    //查看团队
    if (indexPath.section == 1 && indexPath.row == 2) {
        MyTeamViewController *MyTeamVC = [[MyTeamViewController alloc] init];
        [self.navigationController pushViewController:MyTeamVC animated:YES];
        
    }
    
    
    
    //帮助中心
    if (indexPath.section == 2 && indexPath.row == 0) {
        MineHelpCenterViewController *helpCenterVC = [[MineHelpCenterViewController alloc] init];
        [self.navigationController pushViewController:helpCenterVC animated:YES];
    }
    
    //联系我们
    if (indexPath.section == 2 && indexPath.row == 1) {
        [self call];
    }
    
}

//调用系统打电话
-(void)call
{
    if ([AppTools getPhoneNum].length == 0) {
        [self showFailedResultTips:@"未获取到客服电话" animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
    }else
    {
        NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@", [AppTools getPhoneNum]];
        
        NSComparisonResult compare = [[UIDevice currentDevice].systemVersion compare:@"10.0"];
        
        if (compare == NSOrderedDescending || compare == NSOrderedSame) {
            
            /// 大于等于10.0系统使用此openURL方法
            
            if (@available(iOS 10.0, *)) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
            } else {
                // Fallback on earlier versions
            }
            
        } else {
            
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
            
        }
    }
    
    
}

@end
